﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BL.Entity;
using BL.Service;

namespace LTO
{
    /// <summary>
    /// Interaction logic for viewTodGroup.xaml
    /// </summary>
    public partial class viewFourNumber : Window
    {
        tbl_NumberServer Numberservice = new tbl_NumberServer();
        tbl_PayService PayService = new tbl_PayService();
        List<ShowNumberDTO> show4;
        Pay limit;
        decimal LimitNum = 0;
        decimal minSt = 0;
        decimal minEn = 0;
        string NumberType;
        bool IsMinToMax;
        bool IsShowOver;
        bool IsShowDummy;
        bool IsForstLoad = false;

        HotKey _HotKey_Esc;
        HotKey _HotKey_F1;
        HotKey _hotkey_F4;
        HotKey _hotKey_F3;
        HotKey _HotKey_Print;
        HotKey _HotKey_F2;
        HotKey _HotKey_DeleteNum;

        public viewFourNumber()
        {
            InitializeComponent();
            Newload();
            this.Title = "โต๊ดหมู่ 4 ตัว";
             _HotKey_Esc = new HotKey(Key.Escape, KeyModifier.None, OnCloseWindows);
             _HotKey_F1 = new HotKey(Key.F1, KeyModifier.None, Newload);
             _hotkey_F4 = new HotKey(Key.F4, KeyModifier.None, OnHotKeyShowDummy);
             _hotKey_F3 = new HotKey(Key.F3, KeyModifier.None, OnHotKeyShowOverLimit);
             _HotKey_Print = new HotKey(Key.F9, KeyModifier.None, PrintReport);
             _HotKey_F2 = new HotKey(Key.F2, KeyModifier.None, ShowMinToMax);
             _HotKey_DeleteNum = new HotKey(Key.Subtract, KeyModifier.None, DeleteNum);
        }
        public void UnregisterDelete()
        {
            if (_HotKey_DeleteNum != null)
            {
                _HotKey_DeleteNum.Unregister();
            }
        }
        public void RegisterDelete()
        {
            if (_HotKey_DeleteNum != null)
            {
                _HotKey_DeleteNum.Register();
            }
        }
        protected void DeleteNum(HotKey h)
        {
            List<ShowNumberDTO> tempdel = new List<ShowNumberDTO>();
            tempdel.AddRange(show4);

            UnregisterDelete();
            deleteNUm deleteWin = new deleteNUm(tempdel, EnumLocal.NumberType.FourNumber);
            deleteWin.Owner = this;
            deleteWin.Show();
        }
        private void PrintReport(HotKey hotKey)
        {
            List<ShowNumberDTO> print_temp = new List<ShowNumberDTO>();
            print_temp.AddRange(show4);

            try
            {


                Gobalsource.PrintReport(print_temp, EnumLocal.NumberType.FiveNumber, LimitNum);
                Newload();
            }
            catch (Exception)
            {


            }

        }
        protected void ShowMinToMax(HotKey h)
        {
            IsForstLoad = false;
            IsMinToMax = !IsMinToMax;
            LoadData();
        }
        protected void OnHotKeyShowDummy(HotKey h)
        {
            IsShowDummy = !IsShowDummy;
            LoadData();
        }
        protected void OnHotKeyShowOverLimit(HotKey h)
        {
            IsShowOver = !IsShowOver;
            LoadData();
        }
        private void Newload(HotKey h)
        {
            Newload();
        }
        public void Newload()
        {
            IsForstLoad = true;
            limit = PayService.GetLimit(EnumLocal.NumberType.FourNumber);

            LimitNum = limit.LimitBuy;
            minSt = limit.MinStart;
            minEn = limit.MinEnd;
            GetNumberShow();
            if (this.Owner != null)
            {
                ClientMain main = (ClientMain)this.Owner;
                main.ReloadSceen();
            }
        }
        private void OnCloseWindows(HotKey hotKey)
        {
            (Owner as ClientMain).UpdateData();
            this.Close();
        }
        public void GetNumberShow()
        {
            List<NumberDTO> dbNUmber = new List<NumberDTO>();
        


            dbNUmber = Numberservice.get_numberShow(EnumLocal.NumberType.FourNumber).Where(w => w.Tod_Price > 0).ToList();
            show4 = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Tod, LimitNum, minSt, minEn);
            show4 = Gobalsource.ConvertToDummy(show4);
            // btnCut.IsEnabled = show4.Count(c => c.IsOver == true) >= 1 || show5.Count(c => c.IsOver == true) >= 1;

            LoadData();

        }
        protected void LoadData()
        {
            listBox1.ItemsSource = show4.Skip(0).Take(20);
            listBox2.ItemsSource = show4.Skip(20).Take(20);
            labSum.Content = String.Format("{0:#,#}", show4.Sum(s => s.Price));


        }

        private void listBoxPage1_KeyDown_1(object sender, KeyEventArgs e)
        {
            ListBox ls = sender as ListBox;
            ShowNumberDTO select;
            switch (e.Key)
            {

                case Key.OemMinus:

                    select = ls.SelectedItem as ShowNumberDTO;
                    Numberservice.DeleteNumber(select, EnumLocal.NumberType.FourNumber, Gobalsource.UserNameLogin);
                    this.GetNumberShow();
                    break;
                case Key.Subtract:

                    select = ls.SelectedItem as ShowNumberDTO;
                    Numberservice.DeleteNumber(select, EnumLocal.NumberType.FourNumber, Gobalsource.UserNameLogin);
                    this.GetNumberShow();
                    break;

            }
        }

        private void listBoxPage2_KeyDown_1(object sender, KeyEventArgs e)
        {
            ListBox ls = sender as ListBox;
            ShowNumberDTO select;
            switch (e.Key)
            {

                case Key.OemMinus:

                    select = ls.SelectedItem as ShowNumberDTO;
                    Numberservice.DeleteNumber(select, EnumLocal.NumberType.FiveNumber, Gobalsource.UserNameLogin);
                    this.GetNumberShow();
                    break;
                case Key.Subtract:
                    select = ls.SelectedItem as ShowNumberDTO;
                    Numberservice.DeleteNumber(select, EnumLocal.NumberType.FiveNumber, Gobalsource.UserNameLogin);
                    this.GetNumberShow();
                    break;

            }
        }

        private void btnCut_Click(object sender, RoutedEventArgs e)
        {
            tbl_ExportService exportService = new tbl_ExportService();
            exportService.ExportNumber(LimitNum, EnumLocal.NumberType.FourNumber, Gobalsource.UserNameLogin);
            this.GetNumberShow();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            (Owner as ClientMain).UpdateData();
        }
    }
}
